package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.AccountService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */


    @Autowired
    private AccountService accountService;

    /**
     * 登录
     * @param httpSession
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login.action")
    public String check(HttpSession httpSession, @Param("username")String username, @Param("password") String password){
        if("admin".equals(username) && "admin".equals(password)){
            httpSession.setAttribute("username",username);
            return "forward:resumeList.action";
        }else{
            return "forward:login.jsp";
        }
    }

    /**
     * 查询列表
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/resumeList.action",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView  getResumeList(HttpSession httpSession){
        ModelAndView view =new ModelAndView();
        List<Resume> resumes = accountService.queryResumeList();
        view.addObject("resumes",resumes);
        view.setViewName("resumeindex.jsp");
        return view;
    }

    /**
     * 修改
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/editResumeInfo.action", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView editResumeIfo(@RequestParam("id") Long id)  throws Exception{
        ModelAndView view = new ModelAndView();
        Resume resume = accountService.getById(id);
        view.addObject("resume", resume);
        view.setViewName("/resumeedit.jsp");
        return view;
    }

    /**
     * 新增
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addResumeInfo.action", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView addResumeIfo()  throws Exception{
        ModelAndView view = new ModelAndView();
        view.setViewName("/resumeadd.jsp");
        return view;
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteResumeInfo.action", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public String deleteResumeIfo(@RequestParam("id") Long id)  throws Exception{
        accountService.deleteById(id);
        return "success";
    }

    /**
     * 退出
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/loginOut.action", method = { RequestMethod.POST, RequestMethod.GET })
    public String clientLoginOut(HttpSession httpSession){
        httpSession.invalidate();
        return "forward:resumeList.action";
    }

}
